/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.businesslogic;

import java.util.List;

import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.I_Product;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductPrice;

/**
 *
 * @author Administrator
 */
public class ProductUtil {

   public static List<Product> getProductInfo(I_DataLogicSales dlSales,String codeOrReference) throws BasicException{
	   List<Product> oProducts = null;
	   //查询是否为查包
	   if(codeOrReference.length()==14&&codeOrReference.startsWith("1"))
		   oProducts = dlSales.getBomProductForIncludeList(codeOrReference.substring(1));
	   else if(codeOrReference.length()==14&&codeOrReference.startsWith("2"))
		   oProducts = dlSales.getBomIncludeProductList(codeOrReference.substring(1));
	  else
		   oProducts = dlSales.getProductInfoByCode(codeOrReference);

	   Product oProduct;
        //当按条码找不到商品时，如果输入的可能是编号（长度小于9位），则进行按编号查找
       if (oProducts.isEmpty()&&!codeOrReference.isEmpty()&&codeOrReference.length()<9){
        	 oProduct = dlSales.getProductInfoByReference(codeOrReference);
        	 if(oProduct!=null)
        	   oProducts.add(oProduct);
       } 
       return oProducts;
   }
   
   public static void setGroupProductPrice(I_DataLogicSales dlSales,Product oProduct,String priceGroup)throws BasicException{
  	 if(priceGroup!=null){
				ProductPrice groupPrice=dlSales.findProductPriceByGroupAndProduct(priceGroup, oProduct.getID());
			    if(groupPrice!=null){
			    	if (groupPrice.getM_dCustomerPrice() > 0) {
			    		oProduct.setM_dCustomerPrice(groupPrice.getM_dCustomerPrice());
					}
			    	oProduct.setPriceSell(groupPrice.getM_dPriceSell());
			    }
		 }
   }
   
   public static QBFParameters getProductSeach(String sCode){
		QBFParameters productSeachQbfParameters;
		QBFParameter code,ref,name,origin,mnemonic;
		
		code=new QBFParameter(I_Product.CODE);
		ref=new QBFParameter(I_Product.REFERENCE);
		name=new QBFParameter(I_Product.NAME);
		origin=new QBFParameter(I_Product.ORIGIN);
		mnemonic=new QBFParameter(I_Product.MNEMONIC);
		
		productSeachQbfParameters=new QBFParameters();
		productSeachQbfParameters.or(code).or(ref).or(name).or(origin).or(mnemonic);
		
		code.equal(sCode);
		ref.equal(sCode);
		name.link(sCode);
		origin.link(sCode);
		mnemonic.link(sCode);
		
		return productSeachQbfParameters;
	}
   
   
   
}
